package com.gdth.bank.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gdth.bank.R;
import com.gdth.bank.activity.CjkhjlActivity;
import com.gdth.bank.activity.TxlActivity;
import com.gdth.bank.adapter.CjkhLxfsListViewAdapter;
import com.gdth.bank.adapter.CjkhThjlListViewAdapter;
import com.gdth.bank.event.CjkhjlListEvent;
import com.gdth.bank.presenter.CjkhLxfsPresenter;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.view.CjkhLxfsView;
import com.gdth.bank.widget.BankDrawDownListTextView;
import com.gdth.bank.widget.BankEditText;
import com.gdth.bank.widget.DialogMedia;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseFrameLayout;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjLxfs;
import com.gdth.database.bean.TAppBfrjThjl;
import com.gdth.model.Item;
import com.gdth.utils.ImageUtil;
import com.gdth.utils.RegularUtil;
import com.gdth.utils.ToolUtil;
import com.gdth.utils.UUIDUtil;
import com.gdth.widget.dialog.DialogConfig;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import org.greenrobot.eventbus.EventBus;

/**
 * 采集客户联系方式页面
 * @author arvin
 *
 */
public class CjkhLxfsFragment extends BaseFrameLayout implements CjkhLxfsView{
	@ViewInject(id=R.id.lxfs_jtcy) BankDrawDownListTextView drawDownList_jtcy;
	@ViewInject(id=R.id.lxfs_dhhm) BankEditText edit_dhhm;
	@ViewInject(id=R.id.lxfs_lxfs_listview) ListView listview_lxfs;
	@ViewInject(id=R.id.lxfs_thjl_listview) ListView listview_thjl;

	@ViewInject(id=R.id.lxfs_add)
	Button lxfs_add;
	private CjkhjlActivity activity;
	
	private String jcuuid;
	
	CjkhLxfsPresenter presenter;
	
	Drawable bflyDrawable, phoneDrawable, txlDrawable;
	
	
	/** 联系方式适配器 **/
	CjkhLxfsListViewAdapter lxfsAdapter;
	
	/** 通话记录适配器 **/
	CjkhThjlListViewAdapter thjlApapter;
	
	/** 联系方式 **/
	private List<TAppBfrjLxfs> lxfsList;
	
	/** 通话记录方式 **/
	private List<TAppBfrjThjl> thjlList;
	
	List<Item> jtcys;
	
	public TAppBfrjThjl mTAppBfrjThjl;
	DialogConfig config;
	
	public CjkhLxfsFragment(Context context){
		super(context, R.layout.fragment_cjkhjl_lxfs);
		
		this.activity = (CjkhjlActivity) context;
	}
	
	public void initializeView(){
		bflyDrawable = ImageUtil.getImageAsDrawable(this.context, R.drawable.cjkh_bfly);
		phoneDrawable = ImageUtil.getImageAsDrawable(this.context, R.drawable.bank_speak);
		txlDrawable = ImageUtil.getImageAsDrawable(this.context, R.drawable.cjkh_txl);
		
		drawDownList_jtcy.setLabel("家庭成员：");
		edit_dhhm.setLabel(" ");
		edit_dhhm.setHint("请填写电话号码");
		edit_dhhm.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
		edit_dhhm.setImage(txlDrawable);
		edit_dhhm.setOnClickListener(new selectPhoneClickListener());
		
		lxfsList = new ArrayList<TAppBfrjLxfs>();
		lxfsAdapter = new CjkhLxfsListViewAdapter(this.context, lxfsList);
		lxfsAdapter.setOnCallListener(new mOnPhoneCallListener());
		listview_lxfs.setAdapter(lxfsAdapter);

		thjlList = new ArrayList<TAppBfrjThjl>();
		thjlApapter = new CjkhThjlListViewAdapter(this.context, thjlList);
		thjlApapter.setOnCallListener(new mOnPhoneSpeakListener());
		listview_thjl.setAdapter(thjlApapter);
		lxfs_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				save();
			}
		});
	}
	
	public void initializePresenter(){
		presenter = new CjkhLxfsPresenter(this);
	}
	
	public void setJcuuid(String jcuuid) {
		this.jcuuid = jcuuid;

		presenter.getThjlByUuid(jcuuid);
		presenter.getJtcyByUuid(jcuuid);
	}
	
	public void updateJtcy() {
	presenter.getJtcyByUuid(jcuuid);
	}
	private long lastTime = 0;
	/**
	 * 添加电话号码
	 */
	public void save(){
		if(System.currentTimeMillis() - lastTime  < 1000) {
			return ;
		}
		lastTime = System.currentTimeMillis();
		EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.refreshList));
		String phone = edit_dhhm.getValue().toString();
		
//		boolean isMobile = RegularUtil.isMobile(phone);
//		boolean isPhone = RegularUtil.isPhone(phone);
//		if (isMobile && isPhone){
//			BaseToast.getInstance().ShowSystemToast(context, "电话号码格式不正确");
//			return;
//		}
		
		if(!phone.equals("")){
			if(!RegularUtil.telCheck(phone)){
				showMessage("电话号码格式不正确");
				return;
			}
		}else{
			showMessage("电话号码不能为空");
			return;
		}

		TAppBfrjLxfs mTAppBfrjLxfs = new TAppBfrjLxfs();
		
		Object obj = drawDownList_jtcy.getkey();
		if (obj == null || obj.equals("")) {
			showMessage("请选择家庭成员");
			return;
		}
			
		String jtuuid = obj.toString();
		
		mTAppBfrjLxfs.setDhhm(phone);
		mTAppBfrjLxfs.setJtuuid(jtuuid);
		mTAppBfrjLxfs.setCjsj(new Date());
		mTAppBfrjLxfs.setBbh(1);
		mTAppBfrjLxfs.setUuid(UUIDUtil.getUUID());
		mTAppBfrjLxfs.setJcuuid(jcuuid);
		mTAppBfrjLxfs.setSjzt(AppUtil.sjzt_create);
		
		presenter.save(mTAppBfrjLxfs);

		updateJtcy();
	}
	
	/**
	 * 查询通讯录
	 * @author mac1
	 *
	 */
	private class selectPhoneClickListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			Intent intent = new Intent(context, TxlActivity.class);
			activity.startActivityForResult(intent, AppUtil.activity_txl_requestcode);
		}
	}

	/**
	 * 电话拨打事件
	 * @author mac1
	 *
	 */
	private class mOnPhoneCallListener implements CjkhLxfsListViewAdapter.onPhoneCallListener {

		@Override
		public void callPhone(String jtuuid, String number) {
			// TODO Auto-generated method stub
			
			activity.telePhone(jtuuid, number);
			
		}
	};
	


	private int state = 0;
	DialogMedia mDialogMedia;
	/**
	 * 录音播放
	 * @author arvin
	 *
	 */
	private class mOnPhoneSpeakListener implements CjkhThjlListViewAdapter.onPhoneSpeakListener {
		
		@Override
		public void callPhone(String uuid) {
			// TODO Auto-generated method stub
			if(state == 0){
				if (mDialogMedia == null) {
					mDialogMedia = new DialogMedia(context, "播放录音", false);
				}
				String path = ToolUtil.getFileFullPath(ToolUtil.getFilePath(uuid + ".3gp"));
				mDialogMedia.start(path);
				mDialogMedia.show();
			}
		}
	};
	
	/**
	 * 设置电话号码
	 * @param
	 * @param number
	 */
	public void setPhoneNumber(String number) {
		// TODO Auto-generated method stub
		number = number.replaceAll(" ", "");
		edit_dhhm.setValue(number);
	}
	
	
	
	/**
	 * 保存通话记录
	 * @param number
	 * @param jcuuid
	 * @param jtuuid
	 * @param uuid
	 */
	public void savePhoneCall(String number, String jcuuid, String jtuuid, String uuid) {
		
		mTAppBfrjThjl = new TAppBfrjThjl();
		mTAppBfrjThjl.setBfrq(new Date());
		mTAppBfrjThjl.setCjsj(new Date());
		mTAppBfrjThjl.setBbh(1);
		mTAppBfrjThjl.setDhhm(number);
		mTAppBfrjThjl.setJcuuid(jcuuid);
		mTAppBfrjThjl.setJtuuid(jtuuid);
		mTAppBfrjThjl.setLywj(uuid + ".3gp");
		mTAppBfrjThjl.setSjzt(AppUtil.sjzt_create);
		mTAppBfrjThjl.setUuid(uuid);
		
		config = new DialogConfig(context, "是否保存通话记录", true, savePhoneCallListener);
		config.show();
		
	}
	
	/**
	 * 保存通话记录信息
	 */
	private OnClickListener savePhoneCallListener = new  OnClickListener() {
		
		@Override
		public void onClick(View v) {
			presenter.saveThjl(mTAppBfrjThjl, activity.user.getSfz());
			config.dismiss();
		}
		
	};


	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		activity.showProgressDialog(msg);
	}

	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		activity.hideProgressDialog();
	}
	
	@Override
	public void showMessage(String msg){
		activity.showMessage(msg);
	}

	@Override
	public void saveSuccess(Long id) {
		// TODO Auto-generated method stub
//		mTAppBfrjLxfs.setId(id);
//		for (Item item : jtcys) {
//			if (item.getKey().equals(mTAppBfrjLxfs.getJtuuid())) {
//				mTAppBfrjLxfs.setCyxm(item.getValue());
//			}
//		}
//		
//		lxfsList.add(mTAppBfrjLxfs);
//		
//		lxfsAdapter.notifyDataSetChanged();
		
	}
	


	@Override
	public void saveSuccess(TAppBfrjLxfs mTAppBfrjLxfs) {
		// TODO Auto-generated method stub
		for (Item item : jtcys) {
			if (item.getKey().equals(mTAppBfrjLxfs.getJtuuid())) {
				mTAppBfrjLxfs.   setCyxm(item.getValue());
			}
		}
		lxfsList.add(mTAppBfrjLxfs);
		
		lxfsAdapter.notifyDataSetChanged();
		edit_dhhm.setValue("");
	}

	@Override
	public void saveSuccessThjl(Long id) {
		// TODO Auto-generated method stub
		presenter.getThjlByUuid(jcuuid);
	}

	@Override
	public void getAllBrjl(List<TAppBfrjLxfs> arg0) {
		// TODO Auto-generated method stub
		lxfsList.clear();
		for (TAppBfrjLxfs tAppBfrjLxfs : arg0) {
			for (Item item : jtcys) {
				if (item.getKey().equals(tAppBfrjLxfs.getJtuuid())) {
					tAppBfrjLxfs.setCyxm(item.getValue());
				}
			}
			lxfsList.add(tAppBfrjLxfs);
		}
		
		lxfsAdapter.notifyDataSetChanged();
	}

	@Override
	public void getAllThjl(List<TAppBfrjThjl> arg0) {
		// TODO Auto-generated method stub
		thjlList.clear();
		for (TAppBfrjThjl tAppBfrjThjl : arg0) {
			thjlList.add(tAppBfrjThjl);
		}
		
		thjlApapter.notifyDataSetChanged();
	}

	@Override
	public void getAllJtcy(List<TAppBfrjJtcyb> arg0) {
		// TODO Auto-generated method stub
		jtcys = new ArrayList<Item>();
		for (TAppBfrjJtcyb tAppBfrjJtcyb : arg0) {
			Item item = new Item();
			item.setKey(tAppBfrjJtcyb.getUuid());
			item.setValue(tAppBfrjJtcyb.getCyxm());
			
			jtcys.add(item);
		}
		drawDownList_jtcy.setItem(jtcys);
		

		presenter.getLxfsByUuid(jcuuid);
	}
}
